﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace webnhom.backend
{
    public partial class AddArticle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CKContent.Language = "vi";
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "/ckfinder/";
            _FileBrowser.SetupCKEditor(CKContent);
            //
            if (!IsPostBack)
            {
                //LoadData();
            }
        }
        public void LoadData()
        {
            
        }
        private string UploadFile(FileUpload File)
        {
            //string Today = DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString();
            string today = DateTime.Now.ToShortDateString();
            string UploadPath = "/UserFile/Article/";
            string FilePath = UploadPath + DateTime.Now.ToShortDateString().Replace('/', '-') + DateTime.Now.ToLongTimeString().Replace(':', '-') + DateTime.Now.Millisecond + File.FileName;
            //string FilePath = UploadPath + File.FileName;

            /* N?u thu m?c t?n t?i */
            if (Directory.Exists(Server.MapPath(UploadPath)))
            {
                File.SaveAs(Server.MapPath(FilePath));
                //Utils.Help.CreateThumbnail(Server.MapPath(FilePath), 140, 105);
            }
            else
            {
                /* T?o thu m?c n?u chua t?n t?i */
                Directory.CreateDirectory(Server.MapPath(UploadPath));
                File.SaveAs(Server.MapPath(FilePath));
            }

            return FilePath;
        }
        protected void btSave_Click(object sender, EventArgs e)
        {
            string upfile=null;                   
            if (FileUpload_Images.HasFile)
            {
                if (FileUpload_Images.PostedFile.ContentLength > 1049000)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('File ảnh phải nhỏ hơn 1Mb!');", true);
                    return;
                }

                upfile = UploadFile(FileUpload_Images);
            }
            BLL.ArticleHelper.Add_Article(null,txtArticleName.Text,upfile,null,null,txtDescription.Text,CKContent.Text,int.Parse(dropSate.SelectedValue),DateTime.Now,droPhanLoai.SelectedValue);
            Response.Redirect("/ListArticle.html");
        }

        protected void btCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/ListArticle.html");
        }
    }
}